<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-container">
      <my-search @click="search"></my-search>
    </view>
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <!-- <view class="swiper-item">
          <image :src="item.url" mode="aspectFill"></image>
        </view> -->
        <!-- 点击图片，页面要进行跳转，跳转到分包页面 -->
        <navigator :url="`/subpkg/goods_detail/goods_detail?id=${item.id}`" class="swiper-item">
          <image :src="item.url" mode="aspectFill"></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <view class="nav-list">
      <view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClickHandler(item)">
        <image :src="item.url" mode="aspectFill" class="nav-img"></image>
      </view>
    </view>
    
    <view class="floor-list">
      <view class="floor-item" v-for="(item, index) in flowList" :key="index">
        <!-- 楼层标题区域，标题也是图片 -->
        <image :src="item.floor_title.url" mode="aspectFill" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <navigator class="left-img-box" :url="item.product_list[0].navigatorUrl">
            <image :src="item.product_list[0].url" mode="aspectFill" style="width: 223rpx; height: 275rpx;"></image>
          </navigator>
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(item2, index2) in item.product_list" :key="index2" :url="item2.navigatorUrl">
              <image :src="item2.url" mode="aspectFill" style="width: 233rpx; height: 120rpx;" v-if="index2 !== 0"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
  import { onLoad } from '@dcloudio/uni-app';
  import { ref } from 'vue';
  import * as mockData from '../../api/mock.js'
  import MySearch from '../../conponents/my-search/my-search.vue'; // 组件需要引入才能使用
  import { useCartBadge } from '../../mixins/tabbar-badge.js'
  useCartBadge();

  const swiperList = ref([]);
  const navList = ref([]);
  const flowList = ref([]);

  onLoad(() =>{
    getSwiperList();
    getNavList();
    getFlowList();
  })

  // 获取轮播图数据
  function getSwiperList() {
    // 解构出data属性,并重命名为res
    // 服务器原因,这边的请求一直都是404
    // const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
    // if(Object.prototype.toString.call(res) !== '[object Object]' || res?.meta?.status !== 200) {
    //   // uni.$showMsg方法在main中封装
    //   return uni.$showMsg();
    // }
    // swiperList.value = res.message;
    swiperList.value = mockData.swiperList;
  }
  // 获取nav数据
  async function getNavList() {
    // const { data: res } = await uni.$http.get('/api/public/v1/home/catitems');
    // if(Object.prototype.toString.call(res) !== '[object Object]' || res?.meta?.status !== 200) {
    //   return uni.$showMsg();
    // }
    // navList.value = res.message;
    navList.value = mockData.navList;
  }
  // 点击nav每一项
  function navClickHandler(item) {
    if(item.name === '分类') {
      uni.switchTab({
        url: '/pages/cate/cate'
      })
    }
  }
  // 获取楼层数据
  function getFlowList() {
    // const { data: res } = await uni.$http.get('/api/public/v1/home/floordata');
    // if(Object.prototype.toString.call(res) !== '[object Object]' || res?.meta?.status !== 200) {
    //   return uni.$showMsg();
    // }
    // flowList.value = res.message;
    flowList.value = mockData.flowList;
  }
  // 点击搜索
  function search() {
    uni.navigateTo({
      url: '/subpkg/search/search'
    })
  }
</script>

<style lang="scss" scoped>
.search-container {
  // 设置搜索区域的吸顶效果
  position: sticky;
  top: 0;
  z-index: 1;
}
swiper {
  height: 330rpx;
  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-item {
  // height: 500rpx;
  margin-bottom: 15rpx;
}
.floor-title {
  display: flex;
  height: 60rpx;
  width: 100%;
}
.floor-img-box {
  display: flex;
  // gap: 20rpx;
  padding-left: 10rpx;
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
}
</style>
